import json

import jsonpath
import requests
from bs4 import BeautifulSoup

class DouYinParser():
    __req = requests.Session()

    # 返回无水印url
    def noMark(self,copyUrl):
        rep = self.__req.get(copyUrl)
        rUrl = str(rep.url)
        p = '/video/'
        s = rUrl.find(p)
        e = rUrl.find('/',s+len(p))
        vId = rUrl[s+len(p):e]
        rep = self.__req.get('https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids='+vId)
        dict = json.loads(rep.content)
        # playUrl = str(dict['item_list'][0]['video']['play_addr']['url_list'][0])
        # playUrl = playUrl.replace('playwm','play')
        return dict

class KuaiShouParser():
    __req = requests.Session()
    __headers = {
        'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.0 Mobile/14E304 Safari/602.1'}

    # 根据复制的地址 解析出 移动端的播放地址
    # 1.原地址 'https://live.kuaishou.com/u/3xuc3fvitbftkyw/3xdfmwjsii2f459?did=web_244d5b8aa27645318088ea927819e5a9'
    # 2.解析出地址 'https://m.gifshow.com/fw/photo/3xdfmwjsii2f459?did=web_244d5b8aa27645318088ea927819e5a9'
    def __makeMobileUrl(self, copyUrl):
        prefix = 'https://m.gifshow.com/fw/photo/'
        copyUrl = str(copyUrl)
        pattern = '?did='
        index = copyUrl.find(pattern)
        # 地址不合法
        if index < 0:
            return None
        param1 = copyUrl[index + len(pattern):]

        index1 = copyUrl.rfind('/')
        param2 = copyUrl[index1 + 1:index]
        return prefix + param2 + '?did=' + param1

    # 根据移动端地址解析出无水印视频地址
    def getVideoInfo(self, copyUrl):
        res = self.__req.get(copyUrl, headers=self.__headers)
        soup = BeautifulSoup(res.content, 'html.parser')
        scriptText = str(soup.find_all('script')[3])
        s = scriptText.find('{')
        e = scriptText.rfind('}')+1
        jsonStr = scriptText[s:e]
        obj = json.loads(jsonStr)
        return obj["video"]

class PipixiaParser():
    __req = requests.Session()

    # 返回无水印url
    def noMark(self, copyUrl):
        rep = self.__req.get(copyUrl)
        rUrl = str(rep.url)
        p = '/item/'
        s = rUrl.find(p)
        e = rUrl.find('?', s + len(p))
        vId = rUrl[s + len(p):e]
        rep = self.__req.get('https://is-hl.snssdk.com/bds/cell/detail/?cell_id='+vId+'&api_version=1&cell_type=1&iid=4186576981330702&device_id=38165924603&ac=wifi&mac_address=04%3A02%3A1f%3A4a%3A7d%3A62&channel=huawei&aid=1319&app_name=super&version_code=315&version_name=3.1.5&device_platform=android&ssmix=a&device_type=HUAWEI+GRA-CL00&device_brand=HUAWEI&language=zh&os_api=21&os_version=5.0.1&uuid=a0000055c722ef&openudid=473764a4a4b54a2b&manifest_version_code=315&resolution=1080*1794&dpi=480&update_version_code=31550&_rticket=1606025693058&cdid=04edbbfa-b683-4d97-98a3-40b1697516d1&app_region=CN&sys_region=CN&time_zone=Asia%2FShanghai&app_language=ZH&carrier_region&last_channel&last_update_version_code=0&ts=1606025677&as=a111111111111111111111&cp=a000000000000000000000&mas=01907b0a2e8801320ae743996f22df2e7f8c8c8c8c8c8c8c8c8c8c')
        dict = json.loads(rep.content)
        # url = jsonpath.jsonpath(dict,'$...origin_video_download.url_list[0].url')
        return dict


if __name__ == '__main__':
    # vurl = 'https://live.kuaishou.com/u/3xuc3fvitbftkyw/3xdfmwjsii2f459?did=web_244d5b8aa27645318088ea927819e5a9'
    # purl = 'https://v.kuaishouapp.com/s/UNp7rfnx'
    # k = KuaiShouParser()
    # s = k.getVideoInfo(vurl)

    # douyin = DouYinParser()
    # s = douyin.noMark('https://v.douyin.com/J9pQbAp/')
    # print(s)
    # s = 'adkfm   https://v.douyin.com/J9pQbAp/ adsf'
    # a = s.strip(' ')
    # print(a)
    pipixia = PipixiaParser()
    pipixia.noMark('https://h5.pipix.com/s/J9sTh68/')


